{extend name="../../../view/public/base" /}

{block name="css"}
<style>
    body {
        background-color: #F2F2F2;
    }

    .xm-body {
        height: 400px !important;
        overflow-y: scroll !important;
    }

    .layui-form-selected dl {
        height: 200px !important;
        overflow-y: scroll !important;
    }
</style>
{/block}

{block name="body"}
<div style="padding: 20px; background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card" style="padding: 20px">
                <br><br>
                <form class="layui-form" lay-filter="addForm" autocomplete="off">
                    <input type="hidden" id="id" name="id" value="{$data.id? $data.id : 0}"/>
                    <!--分类id-->

                    {if $is_super == false}
                    <div class="layui-form-item">
                        <label class="layui-form-label">拥有角色</label>
                        <div class="layui-input-block" lay-verify='auth'>
                            <div id="auth"  class="xm-select-demo" style="margin: 0 5px 0 0 ;"></div>
                            <input name="rule_ids" id="auth_id" type="hidden" >
                        </div>
                    </div>
                    {/if}


                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input type="text" name="username" lay-verify="required" placeholder="用户名"  onkeyup="this.value=this.value.replace(/[^\w_]/g,'');" autocomplete="off" class="layui-input" value="{$data.username?$data.username:''}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">昵称</label>
                        <div class="layui-input-block">
                            <input type="text" name="nickname" lay-verify="required" placeholder="昵称" autocomplete="off" class="layui-input" value="{$data.nickname?$data.nickname:''}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="password" name="password"  placeholder="密码" autocomplete="off" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电子邮箱</label>
                        <div class="layui-input-block">
                            <input type="email" name="email" placeholder="电子邮箱" autocomplete="off" class="layui-input"  value="{$data.email?$data.email:''}">
                        </div>
                    </div>
                    {if $is_super == false}

                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            {if !empty($data.status)}
                            <input type="radio" name="status"  value="1"  title="正常"   {$data.status==1 ?  'checked':'' }>
                            <input type="radio" name="status"  value="0"  title="封禁"   {$data.status==0 ?  'checked':'' } >

                            {else/}
                            <input type="radio" name="status"  value="1"  title="正常" checked>
                            <input type="radio" name="status"  value="0"  title="封禁"  >

                            {/if}
                        </div>
                    </div>

                    {else/}
                    <input name="status"  type="hidden" value="1" >
                    {/if}


                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="submitBtn">立即提交</button>
                        </div>
                    </div>
                </form>
                <br><br>
            </div>
        </div>
    </div>
</div>

{/block}

{block name="js"}
<script>
    var layer, form, bwajax, upload, $, laydate,manage,xmSelect,eleTree;

    layui.use(['layer', 'form', 'bwajax', 'upload', 'laydate', 'manage', 'xmSelect','eleTree'], function () {
        layer = layui.layer;
        form = layui.form;
        bwajax = layui.bwajax.instance();
        upload = layui.upload;
        $ = layui.jquery;
        laydate = layui.laydate;
        manage = layui.manage;
        xmSelect = layui.xmSelect;
        eleTree = layui.eleTree;

        //添加时间 日期时间选择器
        laydate.render({
            elem: '#add_time'
            ,type: 'datetime'
        });


        var uploadInst = bw_upload(upload, 'pic');//图标上传



//权限下拉多选---------------------------------------------------  start


        bwajax.post("{:Url('manage/role/getRoleTree')}", {
            checked_ids: '{$data.rules? $data.rules : 0}'
            ,disabled: '0',
            is_child:'true'
        }).then(function (response) {
            console.log(response.data);
            //先渲染多选
            var auth = xmSelect.render({
                el: '#auth',
                theme: {
                    color: '#5FB878',
                },
                content: '<div id="ele2" lay-filter="ele2"></div>',
            })


//渲染自定义内容
            var ele = layui.eleTree.render({
                elem: '#ele2',
                data: response.data,
                showCheckbox: true,
                defaultExpandAll: false,
                checkStrictly:true , //严格按照父子不关联
                renderAfterExpand:false //未展开也渲染子节点数据
            });


//初始化数据---------------------------------------------end


            //初始化选中数据需要后台的树菜单传入checked : true属性
            var arr = ele.getChecked(false, true);
            var value = new Array();
            for (i = 0; i < arr.length; i++) {
                value.push(arr[i].id);
            }
            auth.update({
                prop: {
                    name: 'label',
                    value: 'id',
                },
                initValue: value,
                data: arr,
            }).setValue(arr)
            $('#auth_id').val(value.toString());

            //初始化数据---------------------------------------------end

//监听下拉多选的选择
            auth.update({
                on({arr, change, isAdd}) {
                    if (isAdd === false) {
                        //监听取消
                        ele.setChecked(arr.map(item => item.id), true);
                        var value = new Array();
                        for (i = 0; i < arr.length; i++) {
                            value.push(arr[i].id);
                        }
                        $('#auth_id').val(value.toString());
                    }
                },
            });
//监听树的选择
            layui.eleTree.on("nodeChecked(ele2)", function (d) {
                var arr = ele.getChecked(false, true)
                console.log(arr)
                var value = new Array();
                for (i = 0; i < arr.length; i++) {
                    value.push(arr[i].id);
                }
                $('#auth_id').val(value.toString());
                auth.update({
                    prop: {
                        name: 'label',
                        value: 'id',
                    },
                    data: arr,
                }).setValue(arr)

            })

        });


//权限下拉多选---------------------------------------------------  end

        //监听提交按钮
        form.on('submit(submitBtn)', function (obj) {
            data = obj.field;
            var is_super = '{$is_super}';
            console.log(is_super);
          if(is_super!=1){
              if(!data.rule_ids) {layer.msg('请选择角色', {icon: 2});return false}
          }

            if(!data.username) {layer.msg('用户名不能为空', {icon: 2});return false}
            if(!data.nickname) {layer.msg('昵称不能为空', {icon: 2});return false}

            delete data['eleTree-node'];
            delete data.select;
            var post_url = "{:Url('add')}";
            if(data['id']!=0){
                post_url = "{:Url('edit')}";

            }
            //console.log(data);return false;
            //ajax调用后台接口
            bwajax.post(post_url, data)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('提交成功', {icon: 1});
                        submitSuccess();

                    } else {
                        layer.msg(response.data.msg, {icon: 2});

                    }
                })

            return false;
        })
    });


    function submitSuccess() {
        parent.location.reload();//这里调用的是父页面的reload方法
        setTimeout(function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }, 1000);
    }

    // //粉丝
    // function getNum(){
    //
    //     layer.open({
    //         type: 2,
    //         title: '图标列表',
    //         shade: 0.8,
    //         //closeBtn: 0, //不显示关闭按钮
    //         shade: [0],
    //         shadeClose: true,//单机空白处消失
    //         maxmin: true,
    //         //anim: 4, //动画类型
    //         area: ['40%','90%'],
    //         content:"{:Url('public_common/icon')}",
    //         btn: ['选择图标'],
    //         yes: function(index, layero){
    //
    //             var nodeName = window["layui-layer-iframe" + index];//父页面引用
    //             var icon = nodeName.getIcon();
    //             if(icon!=false){
    //                 $('#icon').val(icon);
    //                 layer.close(index);
    //                 //处理重复提交
    //             }
    //
    //
    //
    //
    //
    //         }
    //     });
    // }
</script>
{/block}